﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;

namespace MyDemos.Demos
{
    public partial class DelegateAndEvent : PhoneApplicationPage
    {
        public DelegateAndEvent ()
        {
            InitializeComponent ();
            Heater heater = new Heater ();
            Alarm alarm = new Alarm ();

            heater.Boiled += alarm.MakeAlarm;

            heater.BoilWater ();
            
        }
    }

    public class Heater
    {
        public delegate void BoiledEventHandler (object sender, BoiledEventArg e);
        public event BoiledEventHandler Boiled;


        public class BoiledEventArg : EventArgs
        {
            public readonly int Temperature;

           

            public BoiledEventArg (int pTemperature)
            {
                Temperature = pTemperature;
            }
        }

        public virtual void OnBoiled (BoiledEventArg e)
        {
            if (Boiled != null)
            {
                Boiled (this, e);
            }
        }

        public void BoilWater ()
        {
            for (int i = 0; i < 100; i++)
            {
                if (i > 95)
                {
                    OnBoiled (new BoiledEventArg (i));
                }
            }
        }

    }

    public class Alarm
    {
        public void MakeAlarm (object sender, Heater.BoiledEventArg e)
        {
            Debug.WriteLine ("Alarm temperature : " + e.Temperature);
        }
    }

}